<template>
	<view class="page">
		<view class="title">
			为了更好的为您服务，请您务必填写真实信息
		</view>
		<view class="form">
			<u-form labelPosition="left" :model="customAdvertising" :rules="rules" labelWidth="auto" ref="uForm">
				<u-form-item borderBottom @click="showLeasingManager = true">
					<template #label class="title">
						<span style="color: red;">*</span>
						<span style="margin-right: 20rpx;font-weight: bold;">投放挡位:</span>
					</template>
					<u-input v-model="customAdvertising.gearPosition" border="none" disabled disabledColor="#ffffff"
						placeholder="请选择投放挡位"></u-input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>
				<u-form-item borderBottom>
					<template #label class="title">
						<span style="color: red;">*</span>
						<span style="margin-right: 20rpx;font-weight: bold;">价格:</span>
					</template>
					<u-input v-model="customAdvertising.price" border="none"></u-input>
				</u-form-item>
				<u-form-item borderBottom>
					<template #label class="title">
						<span style="color: red;">*</span>
						<span style="margin-right: 20rpx;font-weight: bold;">剩余位置:</span>
					</template>
					<u-input v-model="customAdvertising.remainingPositions" border="none"></u-input>
				</u-form-item>
				<u-form-item borderBottom>
					<template #label class="title">
						<span style="color: red;">*</span>
						<span style="margin-right: 20rpx;font-weight: bold;">广告标题:</span>
					</template>
					<u-input v-model="customAdvertising.advertisingTitle" border="none"></u-input>
				</u-form-item>
				<u-form-item borderBottom labelPosition="top">
					<template #label class="title">
						<span style="color: red;">*</span>
						<span style="margin-right: 20rpx;font-weight: bold;">广告主图:</span>
					</template>
					<u-upload :fileList="customAdvertising.logo" @afterRead="afterRead" @delete="deletePic" name="1"
						multiple :maxCount="1" :previewFullImage="true"></u-upload>
				</u-form-item>
				<u-form-item label="视频文件" prop="videoFile" borderBottom labelPosition="top">
					<u-upload :fileList="customAdvertising.videoFile" @afterRead="afterRead" @delete="deletePic"
						name="6" multiple :maxCount="1" width="250" height="150">
						<image src="https://cdn.uviewui.com/uview/demo/upload/positive.png" mode="widthFix"
							style="width: 250px;height: 150px;"></image>
					</u-upload>
				</u-form-item>
				<u-form-item label="广告详情" prop="advertisingDesc" borderBottom>
					<u--textarea v-model="customAdvertising.advertisingDesc" placeholder="请输入详情介绍"></u--textarea>
				</u-form-item>
				<u-form-item label="详情图片" prop="descriptPicture" borderBottom>
					<u-upload :fileList="customAdvertising.descriptPicture" @afterRead="afterRead" @delete="deletePic"
						name="1" multiple :maxCount="1" :previewFullImage="true"></u-upload>
				</u-form-item>
			</u-form>

		</view>
		<u-action-sheet :show="showLeasingManager" :actions="actions" title="请选择投放档位"
			@close="showLeasingManager = false" @select="sexSelect">
		</u-action-sheet>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showLeasingManager: false,
				actions: [{
						name: '1',
					},
					{
						name: '2',
					},
					{
						name: '3',
					},
				],
				rules: {
					'userInfo.name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					'userInfo.sex': {
						type: 'string',
						max: 1,
						required: true,
						message: '请选择男或女',
						trigger: ['blur', 'change']
					},
				},
				radio: '',
				switchVal: false,

				// 商户类型
				rowData: null,
				// 自定义广告
				customAdvertising: {
					gearPosition: '',
					price: '',
					remainingPositions: '',
					advertisingTitle: '',
					logo: [{
						url: 'https://cdn.uviewui.com/uview/swiper/1.jpg',
					}],
					videoFile: null,
					advertisingDesc: '',
					descriptPicture: null
				},

			}
		},
		onLoad(option) {
			console.log(option);
			this.rowData = option;
			uni.setNavigationBarTitle({
				title: this.rowData.name + "入驻"
			});
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			// 图片上传
			// 删除图片
			deletePic(event) {
				this.marketEntry.logo.splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								resolve(res.data.data)
							}, 1000)
						}
					});
				})
			},
			sexSelect(e) {
				this.customAdvertising.gearPosition = e.name;
				// this.$refs.uForm.validateField('userInfo.sex')
			},
			onClickItem(e) {
				console.log(e);
				this.current = e.currentIndex
			},
			add() {
				this.dynamicFormData.domains.push({
					label: '域名',
					value: '',
					rules: [{
						'required': true,
						errorMessage: '域名项必填'
					}],
					id: Date.now()
				})
			},
			del(id) {
				let index = this.dynamicLists.findIndex(v => v.id === id)
				this.dynamicLists.splice(index, 1)
			},
			submit(ref) {
				console.log(this.baseFormData);
				this.$refs[ref].validate().then(res => {
					console.log('success', res);
					uni.showToast({
						title: `校验通过`
					})
				}).catch(err => {
					console.log('err', err);
				})
			},
		}
	}
</script>

<style lang="scss">
	.form {

		padding: 0 20rpx;

	}

	.title {
		width: 100%;
		height: 80rpx;
		line-height: 80rpx;
		align-items: center;
		background-color: #ffebeb;
		color: red;
		padding: 0 20rpx;
	}

	.example {
		padding: 15px;
		background-color: #fff;
	}

	.segmented-control {
		margin-bottom: 15px;
	}

	.button-group {
		margin-top: 15px;
		display: flex;
		justify-content: space-around;
	}

	.form-item {
		display: flex;
		align-items: center;
		flex: 1;
	}

	.button {
		display: flex;
		align-items: center;
		height: 35px;
		line-height: 35px;
		margin-left: 10px;
	}
</style>